﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_base';
</cfscript>

<!---会员拍卖额度--->

<!---新版会员被赠送额度--->
<cffunction name="userCreditAuth" access="remote" returntype="numeric">
  <cfquery datasource="#DB#" name="CreditLimit">
  SELECT SUM(credit) AS sumcredit 
  FROM
  user_glftcredit
  WHERE
  uid = '#uid#'
  AND isEnabled=1
  </cfquery>
  <cfif len(CreditLimit.sumcredit) eq 0>
    <cfreturn 0>
  <cfelse>
    <cfreturn CreditLimit.sumcredit>
  </cfif>
</cffunction>

<cffunction name="userglftcreditlist" access="remote" returntype="query">
  <cfquery datasource="#DB#" name="userglftcreditlist">
  SELECT *
  FROM
  user_glftcredit
  WHERE
  uid = '#uid#'
  order by ugcid desc
  </cfquery>
	<cfreturn userglftcreditlist>
</cffunction>



<cffunction name="userCreditAuth0" access="remote" returntype="numeric">
	<!---会员信息验证获得的信用额度--->
  <cfquery datasource="#DB#" name="user_auth">
  select uid,m_auth,e_auth,i_auth
  from user_base
  where uid = '#uid#'
  </cfquery>
  <cfscript>
	CreditLimit = 0;
	//手机号
	if (user_auth.m_auth eq 1){
		CreditLimit = CreditLimit + 1000;
	}
	//EMAIL
	if (user_auth.e_auth eq 1){
		CreditLimit = CreditLimit + 1000;
	}
	//身份证
	if (user_auth.i_auth eq 1){
		CreditLimit = CreditLimit + 3000;
	}
	</cfscript>
	<cfreturn CreditLimit>
</cffunction>

<cffunction name="userCredit" access="remote" returntype="numeric">
	<cfif not isdefined('CreditLimit') or not isnumeric(CreditLimit)>
    <cfinvoke method="userCreditAuth" returnvariable="CreditLimit" uid="#uid#" />
  </cfif>
	<!---保证金X50--->
  <cfinvoke component="app.cfc.deposit" method="getbzjvalue" returnvariable="userdeposit" uid="#uid#" />
  <cfscript>
  if (userdeposit gt 0){
		CreditLimit = CreditLimit + userdeposit * 50;
	}
  </cfscript>
	<cfreturn CreditLimit>
</cffunction>





<!---拍卖额度占用--->
<cffunction name="unpaywon" access="public" returntype="query">
<!---入单未支付--->
<cfquery datasource="#DB#" name="unpaywon">
SELECT
auction_userlastbid.aid
FROM
auction_userlastbid
INNER JOIN auction_userwon ON auction_userwon.wonID = auction_userlastbid.wonID
INNER JOIN auction_userwonpaid ON auction_userwonpaid.paidID = auction_userwon.paidID
WHERE
auction_userlastbid.uid = #session.user.uid# AND
auction_userlastbid.wonID > 0 AND
auction_userwonpaid.pay_status < 2
</cfquery>
<cfreturn unpaywon>
</cffunction>

<cffunction name="userproxy" access="public" returntype="query">
<!---代理价占用--->
<cfquery datasource="#DB#" name="userproxy">
SELECT
auction.aid,
auction.acode,
auction.auction,
bidproxy AS B,
bidproxy_dt AS DT,
'代理价占用' AS yy
FROM
auction_userbidproxy
INNER JOIN auction ON auction.aid = auction_userbidproxy.aid
WHERE
uid = #session.user.uid# AND
limittime > now()
</cfquery>
<cfreturn userproxy>
</cffunction>



<cffunction name="userbid" access="public" returntype="query">
<!---最高价占用--->
<cfinvoke method="unpaywon" returnvariable="unpaywon" />
<cfquery datasource="#DB#" name="userbid">
SELECT
auction.aid,
auction.acode,
auction.auction,
bid AS B,
bid_dt AS DT,
'最高价占用' AS yy
FROM
auction_userlastbid
INNER JOIN auction ON auction.aid = auction_userlastbid.aid
WHERE
uid = #session.user.uid# AND
is_bid = 1 AND wonID = 0
<cfif userproxy.recordcount gt 0>AND auction.aid NOT IN (#valuelist(userproxy.aid)#)</cfif>
<cfif unpaywon.recordcount gt 0>OR (uid = #session.user.uid# AND
is_bid = 1 AND auction.aid IN (#valuelist(unpaywon.aid)#))</cfif>
</cfquery>
<cfreturn userbid>
</cffunction>


<cffunction name="userCreditUse" access="remote" returntype="query">
<!---代理价占用--->
<cfinvoke method="userproxy" returnvariable="userproxy" />
<!---最高价占用--->
<cfinvoke method="userbid" returnvariable="userbid" />
<cfquery datasource="#DB#" name="userCreditUse" dbtype="query">
SELECT * 
FROM userproxy
UNION ALL
SELECT * 
FROM userbid
ORDER by DT DESC
</cfquery>
<cfreturn userCreditUse>
</cffunction>

<!--- 调用存储过程获取用户可用额度 --->
<cffunction name="userCreditLimit" access="remote" returntype="numeric">
<cfstoredproc procedure="new_pro_get_user_ok_quota" dataSource="#DB#"> 
    <cfprocparam type="IN" CFSQLType="cf_sql_varchar" value="#uid#" > 
    <cfprocparam type="OUT" CFSQLType="cf_sql_integer" variable="res"> 
</cfstoredproc> 
  <cfreturn res>
</cffunction>



<!---会员拍卖额度（可用）--->
<cffunction name="userCreditLimit2" access="remote" returntype="numeric">
	<cfif not isdefined('CreditLimit') or not isnumeric(CreditLimit)>
    <cfinvoke method="userCredit" returnvariable="CreditLimit" uid="#uid#" />
  </cfif>
  <!---出代理价--->
  <cfquery datasource="#DB#" name="userbidproxy">
  select aid
  from auction_userbidproxy
  where uid = #uid#
  <cfif isdefined('aid')>AND aid <> #aid#</cfif>
   and limittime > now()
  </cfquery>
  <cfif userbidproxy.recordcount gt 0>
    <cfquery datasource="#DB#" name="usersumbidproxy">
    select sum(bidproxy) as sumbidproxy
    from auction_userbidproxy
    where uid = #uid# and limittime > now()
		<cfif isdefined('aid')>AND aid <> #aid#</cfif>
    </cfquery>
    <cfscript>
		if (isnumeric(usersumbidproxy.sumbidproxy)){
  	CreditLimit = CreditLimit - usersumbidproxy.sumbidproxy;
		}
    </cfscript>
  </cfif>

  <!---未出代理价的竞拍--->
  <cfquery datasource="#DB#" name="userbid">
  select sum(bid) as sumbid 
  from auction_userlastbid
  WHERE
  is_bid = 1
  AND uid = #uid# and wonid=0
  <cfif userbidproxy.recordcount gt 0>AND aid NOT IN (#valuelist(userbidproxy.aid)#)</cfif>
  <cfif isdefined('aid')>AND aid <> #aid#</cfif>
  </cfquery>
  <cfif isnumeric(userbid.sumbid)>
		<cfset CreditLimit = CreditLimit - userbid.sumbid>
  </cfif>

	<!---入单未支付--->
  <cfquery datasource="#DB#" name="unpaywon">
  SELECT
  Sum(auction_userlastbid.bid) as sumupw
  FROM
  auction_userlastbid
  INNER JOIN auction_userwon ON auction_userwon.wonID = auction_userlastbid.wonID
  INNER JOIN auction_userwonpaid ON auction_userwonpaid.paidID = auction_userwon.paidID
  WHERE
  auction_userlastbid.uid = #uid# AND
  auction_userlastbid.wonID > 0 AND
  auction_userwonpaid.pay_status < 2
  <cfif isdefined('aid')>AND auction_userlastbid.aid <> #aid#</cfif>
  </cfquery>
  <cfif isnumeric(unpaywon.sumupw)>
		<cfset CreditLimit = CreditLimit - unpaywon.sumupw>
  </cfif>
	<cfreturn CreditLimit>
</cffunction>


</cfcomponent>